package com.zhongfl.crm.lixiao.bean.resolve;

import com.heptagram.lixiao.bean.response.customer.LXCustomerListResponse;
import com.zhongfl.crm.model.LxUser;
import com.zhongfl.crm.service.LxUserService;
import tech.heptagram.channel.api.model.Channel;

import java.util.Objects;

/**
 * CustomerModelResolver 服务类
 *
 * @author tiantian
 * @date 2018/7/12 10:00
 */
public class CustomerModelResolver implements ModelResolver<Channel, LXCustomerListResponse.Customer> {

    private LxUserService lxUserService;

    public CustomerModelResolver(LxUserService lxUserService) {
        this.lxUserService = lxUserService;
    }

    @Override
    public Channel resolve(LXCustomerListResponse.Customer customer) throws ModelResolverException {
        Channel channel = new Channel();

        LxUser lxUser = lxUserService.findByLxId(customer.getUserId());
        if (Objects.nonNull(lxUser)) {
            if(Objects.nonNull(lxUser.getStaffName())){
                channel.setStaffName(lxUser.getStaffName());
            }else{
                channel.setStaffName(customer.getName());
            }
            channel.setStaffCode(lxUser.getStaffCode());
            //channel.setStaffMobile();
        }
        if (customer.getContacts().size() > 0) {
            channel.setContactsName(customer.getContacts().get(0).getName());
            channel.setContactsMobile(customer.getContacts().get(0).getAddress().getPhone());
        }
        channel.setChannelName(customer.getName());
        channel.setAddress(customer.getAddress().getDetailAddress());
        channel.setType("CUSTOMER");
        //channel.setLxLeadId();
        channel.setLxCustomerId(customer.getId());
        channel.setCreatedBy("BOSS系统");
        channel.setCreatedTime(customer.getCreatedAt());
        channel.setModifiedBy("BOSS系统");
        channel.setModifiedTime(customer.getUpdatedAt());

        return channel;


    }
}
